<template>
<div>
     <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="用户名" prop="name">
        <el-input v-model="dataForm.name" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="dataForm.password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="dataForm.address"
          placeholder="地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-input
          v-model="dataForm.gender"
          placeholder="性别"
        ></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="dept">
        <el-input
          v-model="dataForm.dept"
          placeholder="部门"
        ></el-input>
      </el-form-item>
      
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具 js，第三方插件 js，json 文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
//import 引入的组件需要注入到对象中才能使用
 name: "updateAndAdd",
components: {},
props: {},
data() {
//这里存放数据
return {
    visible: false,
      dataForm: {
        id:null,
        name:"",
        address:"",
        password:"",
        age:"",
        dept:"",
        gender:"",

      },
       dataRule: {
        uName: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
        uPassword: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ],
        },
};
},
//计算属性 类似于 data 概念
computed: {},
//监控 data 中的数据变化
watch: {},
//方法集合
methods: {
    init(id) {
      // console.log("修改或新增")
      this.dataForm.id= id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
           this.$axios.get(`/staff/selectById/${this.dataForm.id}`)
          .then(({ data }) => {
            if (data && data.code ===200) {
              console.log("data.user", data);
              this.dataForm.id = data.data.id;
              this.dataForm.name=data.data.name;
              this.dataForm.password = data.data.password;
              this.dataForm.address = data.data.address;
              this.dataForm.gender = data.data.gender;
              this.dataForm.age = data.data.age;
              this.dataForm.dept = data.data.dept;
            }

            console.log(this.dataForm);
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
         if(this.dataForm.id){
            this.$axios.put("/staff/update",this.dataForm).then(({data})=>{
                if (data && data.code === 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
            })
         }else{
             this.$axios.post("/staff/save",this.dataForm).then(({data})=>{
                if (data && data.code === 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
            })
         } 
        }
      });
    },
    // change(index, row) {
    //   console.log(row.uStatus);
    // },
  },
//生命周期 - 创建完成（可以访问当前 this 实例）
created() {

},
//生命周期 - 挂载完成（可以访问 DOM 元素）
mounted() {

},
beforeCreate() {}, //生命周期 - 创建之前
beforeMount() {}, //生命周期 - 挂载之前
beforeUpdate() {}, //生命周期 - 更新之前
updated() {}, //生命周期 - 更新之后
beforeDestroy() {}, //生命周期 - 销毁之前
destroyed() {}, //生命周期 - 销毁完成
activated() {}, //如果页面有 keep-alive 缓存功能，这个函数会触发 
}
</script>
<style scoped>

</style>